package com.kaka.dao;

import com.kaka.entity.Adimg;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class AdimgDao {
    public List<Adimg> getList() {
        List<Adimg> list = new ArrayList<>();
        // 操作数据库的代码
        try {
            // 链接上数据库
            Class.forName("com.mysql.jdbc.Driver");
            // 获取数据库的链接
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_maven?useUnicode=true&characterEncoding=utf8",
                    "root", "");
            // 准备sql语句
            String sql = "select * from adimg ORDER BY img_order ASC LIMIT 0,4";
            // 预编译sql语句
            PreparedStatement pstmt = conn.prepareStatement(sql);
            // 执行sql语句
            ResultSet rs = pstmt.executeQuery();
            // 使用循环的方式从rs中获取数据
            while (rs.next()) {
                Adimg img = new Adimg();
                img.setId(rs.getInt("id"));
                img.setImg_url(rs.getString("img_url"));
                img.setImg_desc(rs.getString("img_desc"));
                img.setImg_page(rs.getString("img_page"));
                img.setImg_order(rs.getInt("img_order"));
                list.add(img);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}